/************************************************************
*                                                           *
*   Acknowledgement                                         *
*                                                           *
*   I EXPLICITLY inform that                                *
*   THIS CLASS IS BASED ON THE IMPLEMENTATION               *
*   of Vijay Mathew Pandyalakal.                            *
*   You can find the original source code and               *
*   explanation from Vijay Mathew Pandyalakal               *
*   in the follwoing link.                                  *
*   http://www.codeproject.com/KB/IP/CppHttpWebForm.aspx    *
*                                                           *
*   April, 2010 Seokhwan Kim                                *
*                                                           *
*************************************************************/

#ifndef __INKLUDE_C_WEB_FORM_H__
#define __INKLUDE_C_WEB_FORM_H__

#include <Inklude/Inklude.h>
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <vector>
#include <string>
#include <Inklude/ikCString.h>

namespace Inklude
{
    class URLEncoder {
    public:
	    static std::string encode(std::string str);
    private:
	    static bool isOrdinaryChar(char c);
    };

    class URLDecoder {
    public:
	    static std::string decode(std::string str);
    private:
	    static int convertToDec(const char* hex);
	    static void getAsDec(char* hex);
    };

    class INKLUDE_CLASS_EXPORT ikCWebFormWinAPI {
    private:
        std::string m_userName;
        std::string m_password;
        std::vector<std::string> m_vctVars;
	    std::vector<std::string> m_vctVals;
	    std::string m_strHostName;
	    std::string m_strScriptFile;
	    HINTERNET m_hSession;	
	    HINTERNET m_hRequest;
	    unsigned long m_lBytesRead;

    public:
	    ikCWebFormWinAPI ();
	    void PutVariable(const char* VariableName, const char* Value);
	    std::string GetVariable(const char* VariableName) const;
	    void SetHost(const char* HostName);
	    void SetScriptFile(const char* ScriptFilePath);
	    std::string GetHost() const;
	    std::string GetScriptFile() const;
	    bool SendRequest();
	    int  GetResponse(void* pBuffer, unsigned int nResponseSize, float fTimeOut = 0.f);
	    unsigned long GetBytesRead();
        void Reset();
        void SetUserName(const char* UserName);
        void SetPassword(const char* Password);
	    ~ikCWebFormWinAPI();

    private:
	    bool isDuplicateVar(const char* var_name);
    };

    class ikCWebFormWinAPIException {
    private:
	    std::string err;
    public:
	    ikCWebFormWinAPIException(const char* str) {
		    err = str;
	    }
	    std::string getMessage() {
		    return err;
	    }
    };
}

#endif // __INKLUDE_C_WEB_FORM_H__

